package net.onecloud.onecloud.account;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.application.App;
import net.onecloud.onecloud.base.BaseAppCompatActivity;
import net.onecloud.onecloud.model.ResultUser;
import net.onecloud.onecloud.model.User;
import net.onecloud.onecloud.network.NetWork;
import net.onecloud.onecloud.utils.LogUtils;
import net.onecloud.onecloud.utils.PreferencesUtils;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class LoginActivity extends BaseAppCompatActivity {

    private EditText mUsername;
    private EditText mPassword;
    private Button mLogin;
    private TextView mRegister;

    Observer<ResultUser> logObserver = new Observer<ResultUser>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e(e);
        }

        @Override
        public void onNext(ResultUser resultUser) {
            if (resultUser.getSuccess() == 1) {
                ResultUser.DataEntity user = resultUser.getData();
                if (user != null) {
                    App.setUser(user);
                    setResult(RESULT_OK);
                    showHitMustFinish("登录成功");
                }
            }
            if (resultUser.getSuccess() == 2) {
                showHit("账号或者密码不正确,请重新输入");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setToolBarTitle("登录");
        HideLeftButton(false);
        initViews();
        initListener();
        initData();
    }


    private void initData() {
    }

    private void initListener() {
        mLogin.setOnClickListener(this);
        mRegister.setOnClickListener(this);
    }

    private void initViews() {
        mUsername = (EditText) findViewById(R.id.m_username);
        mPassword = (EditText) findViewById(R.id.m_password);
        mLogin = (Button) findViewById(R.id.m_bt_login);
        mRegister = (TextView) findViewById(R.id.m_register);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.m_bt_login: {
                startLogin();
                break;
            }
            case R.id.m_register: {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 0);
                break;
            }
        }
    }

    private void startLogin() {
        String username = mUsername.getText().toString();
        String password = mPassword.getText().toString();
        String info = "";
        if (TextUtils.isEmpty(username)) {
            info = "账号不能为空";
        }
        if (TextUtils.isEmpty(password)) {
            info = "密码不能为空";
        }
        if (!info.equals("")) {
            showHit(info);
            return;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        NetWork.getAccountService()
                .login(user)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(logObserver);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (resultCode == RESULT_OK) {
            ResultUser.DataEntity user = data.getExtras().getParcelable("user");
            if (user != null) {
                mUsername.setText(user.getUsername());
                mPassword.setText(user.getPassword());
            }

        }
    }
}
